/**
 * Created by superman on 17/2/16.
 * http配置
 */

import axios from 'axios'
import {Toast} from 'mint-ui'
// axios 配置
axios.defaults.timeout = 5000
axios.defaults.baseURL = 'http://api.chat.com/api'

// http request 拦截器
axios.interceptors.request.use(
  config => {
    if (localStorage.getItem('auth-token')) {
      config.headers.Authorization = `Bear ${localStorage.getItem('auth-token')}`
    }
    return config
  },
  err => {
    return Promise.reject(err)
  },
)

// http response 拦截器
axios.interceptors.response.use(
  response => {
    let res = response.data;
    switch (res.code) {
      case 200:
        return res.data;
        break;
      case 401:

        break;
      default:
        Toast({
          message:res.msg,
          position:'middle',
          duration:5000
        })
    }
    // return response
  },
  error => {
    if (error.response) {
      switch (error.response.status) {
        case 401:
          // 401 清除token信息并跳转到登录页面
          
          // store.commit(types.LOGOUT)

          // 只有在当前路由不是登录页面才跳转
          // router.currentRoute.path !== 'login' &&
          //   router.replace({
          //     path: 'login',
          //     query: { redirect: router.currentRoute.path },
          //   }),
            break;
        case 404:
            Toast({
              message:'404',
              position:'middle',
              duration:5000
            })
            break;
      }
    }
    // console.log(JSON.stringify(error));//console : Error: Request failed with status code 402
    return Promise.reject(error.response.data)
  },
)

export default axios
